package com.baozheyangken.ai_learn.service.serviceImpl;

import com.baozheyangken.ai_learn.pojo.entity.Question;
import com.baozheyangken.ai_learn.service.PracticeService;
import com.baozheyangken.ai_learn.mapper.QuestionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PracticeServiceImpl implements PracticeService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<Question> IntelligentTest(Map<String, Object> params) {
        try {
            log.info("智能测试请求，参数: {}", params);
            
            // 获取课程ID（修改：从knowledgeId改为courseId）
            Integer courseId = (Integer) params.get("courseId");
            if (courseId == null) {
                log.warn("智能测试缺少课程ID参数");
                return List.of();
            }
            
            List<Question> testQuestions = new ArrayList<>();
            
            // 1. 获取选择题 (type_id = 1) - 按难度分配
            // 1.1 获取3道简单选择题 (difficulty_id = 1)
            List<Question> easyMultipleChoice = questionMapper.getQuestionsByCourseIdTypeAndDifficulty(courseId, 1, 1, 3);
            testQuestions.addAll(easyMultipleChoice);
            log.info("获取到{}道简单选择题", easyMultipleChoice.size());
            
            // 1.2 获取1道中等选择题 (difficulty_id = 2)
            List<Question> mediumMultipleChoice = questionMapper.getQuestionsByCourseIdTypeAndDifficulty(courseId, 1, 2, 1);
            testQuestions.addAll(mediumMultipleChoice);
            log.info("获取到{}道中等选择题", mediumMultipleChoice.size());
            
            // 1.3 获取1道困难选择题 (difficulty_id = 3)
            List<Question> hardMultipleChoice = questionMapper.getQuestionsByCourseIdTypeAndDifficulty(courseId, 1, 3, 1);
            testQuestions.addAll(hardMultipleChoice);
            log.info("获取到{}道困难选择题", hardMultipleChoice.size());
            
            // 2. 获取判断题 (type_id = 3)
            List<Question> trueFalseQuestions = questionMapper.getQuestionsByCourseIdAndType(courseId, 3, 3);
            testQuestions.addAll(trueFalseQuestions);
            log.info("获取到{}道判断题", trueFalseQuestions.size());
            
            // 3. 获取填空题 (type_id = 2)
            List<Question> fillBlankQuestions = questionMapper.getQuestionsByCourseIdAndType(courseId, 2, 2);
            testQuestions.addAll(fillBlankQuestions);
            log.info("获取到{}道填空题", fillBlankQuestions.size());
            
            // 不进行随机打乱，保持原有顺序
            log.info("智能测试生成完成，共{}道题目，按顺序返回", testQuestions.size());
            return testQuestions;
            
        } catch (Exception e) {
            log.error("智能测试生成失败: {}", e.getMessage(), e);
            return List.of();
        }
    }

    @Override
    public List<Question> getQuestions(Map<String, Object> params) {
        try {
            log.info("练习模块获取题目，参数: {}", params);
            
            // 检查是否有知识点ID参数
            if (params.containsKey("knowledgeId")) {
                Integer knowledgeId = (Integer) params.get("knowledgeId");
                List<Question> questions = questionMapper.getQuestionsByKnowledgeId(knowledgeId);
                log.info("根据知识点ID {}成功获取{}道题目", knowledgeId, questions.size());
                return questions;
            } else {
                // 如果没有知识点ID，使用原有的getAllQuestions方法
                List<Question> questions = questionMapper.getAllQuestions(params);
                log.info("成功获取{}道题目", questions.size());
                return questions;
            }
        } catch (Exception e) {
            log.error("获取题目失败: {}", e.getMessage(), e);
            return List.of();
        }
    }

}